#include "user_add_dialog.h"
#include "com/virt_kb/virt_kb.h"

// 外部变量声明
extern virt_kb_t *g_virt_kb;

// 前向声明
static void btn_ok_event_cb(lv_event_t* e);
static void btn_cancel_event_cb(lv_event_t* e);
static void ta_event_cb(lv_event_t* e);

// 创建添加用户对话框
user_add_dialog_t* user_add_dialog_create(lv_obj_t* parent, void (*close_cb)(void)) {
    // 分配内存
    user_add_dialog_t* dialog = (user_add_dialog_t*)lv_mem_alloc(sizeof(user_add_dialog_t));
    if (dialog == NULL) {
        return NULL;
    }
    lv_memset(dialog, 0, sizeof(user_add_dialog_t));

    dialog->close_cb = close_cb;

    // 创建对话框
    dialog->obj = lv_obj_create(parent);
    lv_obj_set_size(dialog->obj, 340, 260);
    lv_obj_center(dialog->obj);
    lv_obj_set_style_pad_all(dialog->obj, 20, 0);

    // 创建标题
    lv_obj_t* title = lv_label_create(dialog->obj);
    lv_label_set_text(title, "Add New User");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 0);

    // 创建用户名输入区域的容器
    lv_obj_t* username_cont = lv_obj_create(dialog->obj);
    lv_obj_set_size(username_cont, 300, 40);
    lv_obj_align(username_cont, LV_ALIGN_TOP_LEFT, 0, 50);
    lv_obj_set_style_pad_all(username_cont, 0, 0);
    lv_obj_set_style_border_width(username_cont, 0, 0);
    lv_obj_set_flex_flow(username_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(username_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建用户名标签和输入框
    lv_obj_t* username_label = lv_label_create(username_cont);
    lv_label_set_text(username_label, "Username:");
    lv_obj_set_style_min_width(username_label, 80, 0);

    dialog->username_ta = lv_textarea_create(username_cont);
    lv_obj_set_size(dialog->username_ta, 180, 40);
    lv_textarea_set_placeholder_text(dialog->username_ta, "Enter username");
    lv_obj_add_event_cb(dialog->username_ta, ta_event_cb, LV_EVENT_ALL, dialog);

    // 创建密码输入区域的容器
    lv_obj_t* password_cont = lv_obj_create(dialog->obj);
    lv_obj_set_size(password_cont, 300, 40);
    lv_obj_align(password_cont, LV_ALIGN_TOP_LEFT, 0, 110);
    lv_obj_set_style_pad_all(password_cont, 0, 0);
    lv_obj_set_style_border_width(password_cont, 0, 0);
    lv_obj_set_flex_flow(password_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(password_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建密码标签和输入框
    lv_obj_t* password_label = lv_label_create(password_cont);
    lv_label_set_text(password_label, "Password:");
    lv_obj_set_style_min_width(password_label, 80, 0);

    dialog->password_ta = lv_textarea_create(password_cont);
    lv_obj_set_size(dialog->password_ta, 180, 40);
    lv_textarea_set_placeholder_text(dialog->password_ta, "Enter password");
    lv_textarea_set_password_mode(dialog->password_ta, true);
    lv_obj_add_event_cb(dialog->password_ta, ta_event_cb, LV_EVENT_ALL, dialog);

    // 创建用户类型区域的容器
    lv_obj_t* type_cont = lv_obj_create(dialog->obj);
    lv_obj_set_size(type_cont, 300, 40);
    lv_obj_align(type_cont, LV_ALIGN_TOP_LEFT, 0, 170);
    lv_obj_set_style_pad_all(type_cont, 0, 0);
    lv_obj_set_style_border_width(type_cont, 0, 0);
    lv_obj_set_flex_flow(type_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(type_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建用户类型标签和下拉框
    lv_obj_t* type_label = lv_label_create(type_cont);
    lv_label_set_text(type_label, "User Type:");
    lv_obj_set_style_min_width(type_label, 80, 0);

    dialog->type_dd = lv_dropdown_create(type_cont);
    lv_dropdown_set_options(dialog->type_dd, "Normal\nAdmin");
    lv_obj_set_size(dialog->type_dd, 180, 40);

    // 创建按钮容器
    lv_obj_t* btn_container = lv_obj_create(dialog->obj);
    lv_obj_set_size(btn_container, 260, 50);
    lv_obj_align(btn_container, LV_ALIGN_BOTTOM_MID, 0, -10);
    lv_obj_set_flex_flow(btn_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(btn_container, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_all(btn_container, 0, 0);
    lv_obj_set_style_border_width(btn_container, 0, 0);

    // 创建确认按钮
    lv_obj_t* btn_ok = lv_btn_create(btn_container);
    lv_obj_set_size(btn_ok, 120, 40);
    lv_obj_add_event_cb(btn_ok, btn_ok_event_cb, LV_EVENT_CLICKED, dialog);
    lv_obj_t* label_ok = lv_label_create(btn_ok);
    lv_label_set_text(label_ok, "OK");
    lv_obj_center(label_ok);

    // 创建取消按钮
    lv_obj_t* btn_cancel = lv_btn_create(btn_container);
    lv_obj_set_size(btn_cancel, 120, 40);
    lv_obj_add_event_cb(btn_cancel, btn_cancel_event_cb, LV_EVENT_CLICKED, dialog);
    lv_obj_t* label_cancel = lv_label_create(btn_cancel);
    lv_label_set_text(label_cancel, "Cancel");
    lv_obj_center(label_cancel);

    return dialog;
}

// 输入框事件回调
static void ta_event_cb(lv_event_t* e) {
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* ta = lv_event_get_target(e);

    if (code == LV_EVENT_FOCUSED) {
        if (g_virt_kb) {
            virt_kb_show(g_virt_kb, ta);
        }
    } else if (code == LV_EVENT_DEFOCUSED) {
        if (g_virt_kb) {
            virt_kb_hide(g_virt_kb);
        }
    }
}

// 确认按钮事件回调
static void btn_ok_event_cb(lv_event_t* e) {
    user_add_dialog_t* dialog = (user_add_dialog_t*)lv_event_get_user_data(e);
    if (!dialog) return;

    const char* username = lv_textarea_get_text(dialog->username_ta);
    const char* password = lv_textarea_get_text(dialog->password_ta);
    uint16_t type = lv_dropdown_get_selected(dialog->type_dd);

    // 验证输入
    if (strlen(username) == 0 || strlen(password) == 0) {
        lv_obj_t* mbox = lv_msgbox_create(NULL, "Error", "Please enter username and password", NULL, true);
        lv_obj_center(mbox);
        return;
    }

    // 检查用户名是否已存在
    UserInfo user_info;
    if (usermgr_get_user_info(username, &user_info)) {
        lv_obj_t* mbox = lv_msgbox_create(NULL, "Error", "Username already exists", NULL, true);
        lv_obj_center(mbox);
        return;
    }

    // 添加用户，使用当前登录用户的凭据作为操作者
    const char* operator_username = sysmgr_get_username();
    const char* operator_password = sysmgr_get_password();
    if (usermgr_add_user(username, password, type, operator_username, operator_password)) {
        // 关闭对话框
        if (dialog->close_cb) {
            dialog->close_cb();
        }
        lv_obj_del(dialog->obj);
        lv_mem_free(dialog);
    } else {
        lv_obj_t* mbox = lv_msgbox_create(NULL, "Error", "Failed to add user", NULL, true);
        lv_obj_center(mbox);
    }
}

// 取消按钮事件回调
static void btn_cancel_event_cb(lv_event_t* e) {
    user_add_dialog_t* dialog = (user_add_dialog_t*)lv_event_get_user_data(e);
    if (!dialog) return;

    // 关闭对话框
    if (dialog->close_cb) {
        dialog->close_cb();
    }
    lv_obj_del(dialog->obj);
    lv_mem_free(dialog);
}